/**
 * 
 */
package be.redo.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import be.redo.model.entities.Airport;
import be.redo.model.entities.Line;

/**
 * @author Frank
 * 
 */
@Repository
public class LineDAOImpl implements LineDAO {
	private EntityManager entityManager;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	/**
	 * @see be.redo.dao.LineDAO#create(be.redo.model.entities.Line)
	 */
	@Override
	public void create(Line instance) {
		try {
			entityManager.persist(instance);
		} catch (RuntimeException ex) {
			entityManager.clear();
			throw ex;
		}
	}

	/**
	 * @see be.redo.dao.LineDAO#read(long)
	 */
	@Override
	public Line read(long id) {
		return entityManager.find(Line.class, id);
	}

	/**
	 * @see be.redo.dao.LineDAO#update(be.redo.model.entities.Line)
	 */
	@Override
	public void update(Line instance) {
		try {
			entityManager.merge(instance);
			entityManager.flush();
		} catch (RuntimeException ex) {
			entityManager.clear();
			throw ex;
		}
	}

	/**
	 * @see be.redo.dao.LineDAO#delete(long)
	 */
	@Override
	public void delete(long id) {
		Line instance = entityManager.find(Line.class, id);
		if (instance != null) {
			entityManager.remove(instance);
			entityManager.flush();
		}
	}

	/**
	 * @see be.redo.dao.LineDAO#findAll()
	 */
	@Override
	public List<Line> findAll() {
		TypedQuery<Line> query = entityManager.createNamedQuery("findAllLines",
				Line.class);
		return query.getResultList();
	}

	/**
	 * @see be.redo.dao.LineDAO#count()
	 */
	@Override
	public int count() {
		TypedQuery<Number> query = entityManager.createNamedQuery(
				"findAantalLines", Number.class);
		return (query.getSingleResult()).intValue();
	}

	/**
	 * @see be.redo.finders.LineFinders#findLinesByDeparture(be.redo.model.entities.Airport)
	 */
	@Override
	public List<Line> findLinesByDeparture(Airport departure) {
		TypedQuery<Line> query = entityManager.createNamedQuery(
				"findLinesByDeparture", Line.class);
		query.setParameter("departure", departure);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.LineFinders#findLinesByDestination(be.redo.model.entities.Airport)
	 */
	@Override
	public List<Line> findLinesByDestination(Airport destination) {
		TypedQuery<Line> query = entityManager.createNamedQuery(
				"findLinesByDestination", Line.class);
		query.setParameter("destination", destination);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.LineFinders#findLinesByDepartureAndDestination(be.redo.model.entities.Airport,
	 *      be.redo.model.entities.Airport)
	 */
	@Override
	public List<Line> findLinesByDepartureAndDestination(Airport departure,
			Airport destination) {
		TypedQuery<Line> query = entityManager.createNamedQuery(
				"findLinesByDepartureAndDestination", Line.class);
		query.setParameter("departure", departure);
		query.setParameter("destination", destination);
		return query.getResultList();
	}

}
